﻿var UI_SELECTED = 'ui-state-focus';
function RowSelectionHandler(options) {
	var _this = this;
	//this.selections = new Map();
	this.prev = null;
	this.grid = null;
	
	this.rowClicked = function(e) {
		var row = getFirstAncestorOfType(e.target, 'TR');
		if (!$(row).hasClass('dt-table-row')) return;
		
		if (options == null ||options.mode == 'single') {
			 _this.grid.getDataRows().removeClass(UI_SELECTED);
			 $(row).addClass(UI_SELECTED);
		} else if (options.mode == 'multiple') {
			if (e.ctrlKey) {
				$(row).toggleClass(UI_SELECTED);
			} else if (e.shiftKey) {
				var from = _this.prev.get(0).rowIndex, to = row.rowIndex;
				_this.grid.getDataRows().removeClass(UI_SELECTED);
				
				var t = row.parentNode.parentNode;
				for (var i=Math.min(from, to);i < Math.max(from, to)+1;i++) {
					if ($(t.rows[i]).hasClass('dt-table-row')) {
						$(t.rows[i]).addClass(UI_SELECTED);
					}
				}
			} else {
				_this.grid.getDataRows().removeClass(UI_SELECTED);
				$(row).addClass(UI_SELECTED);
			}
		}
		_this.prev = $(row);
		$(row).removeClass('ui-state-hover');
		if (options!=null && options.handler!=null) options.handler(_this.selections, null);
	};
	
	this.bind = function(unbind) {
		if (unbind == true) {
			_this.grid.getDataRows().unbind('click', _this.rowClicked);
		} else {
			_this.grid.getDataRows().bind('click', _this.rowClicked);
		}
	};
	
	this.detach = function() {
		this.bind(true);
		this.grid.getDataRows().removeClass(UI_SELECTED);
		this.grid.pageContentChanged.remove(this.bind);
	};
	
	this.attach = function(grid) {
		this.grid = grid;
		this.bind();
		this.grid.pageContentChanged.add(this.bind);
	};
}; RowSelectionHandler.inherits(DatagridPlugin);

function RowHoverHandler() {
	var _this = this;
	this.lastRow = null;
	this.grid = null;
	
	this.onMouseEnter = function(e) {
		var row = getFirstAncestorOfType(e.target, 'TR');
		if (!$(row).hasClass('ui-state-active')) $(row).addClass('ui-state-hover');
	};
	this.onMouseLeave = function(e) {
		var row = getFirstAncestorOfType(e.target, 'TR');
		$(row).removeClass('ui-state-hover');
	};
	this.bind = function(unbind) {
		var rows = _this.grid.getDataRows();
		if (unbind == true) {
			rows.unbind('mouseenter', _this.onMouseEnter);
			rows.unbind('mouseleave', _this.onMouseLeave);
		} else {
			rows.bind('mouseenter', _this.onMouseEnter);
			rows.bind('mouseleave', _this.onMouseLeave);
		}
	};
	
	this.detach = function() {
		this.grid.pageChanged.remove(this.bind);
		this.bind(true);
	};
	
	this.attach = function(grid) {
		this.grid = grid;
		this.grid.pageChanged.add(this.bind);
		this.bind();
	};
}; RowHoverHandler.inherits(DatagridPlugin);


function CellNavigateHandler() {
	var _this = this;
	this.grid = null;
	this.v = function(cell, fwd) {
		_this.before = this.grid.getBody().get(0).scrollTop;
		$(cell.parentNode.parentNode.rows[cell.parentNode.rowIndex+ (fwd  ? 1: -1)].cells[cell.cellIndex]).focus();
	};
	this.h = function(cell, fwd) {
		(fwd ? $(cell).next() : $(cell).prev()).focus();
	};
	this.keyPressed = function(e) {
		var cell = getFirstAncestorOfType(e.target, 'TD');
		switch (e.keyCode) {
			case 13: _this.v(cell, true); break;//$(cell).dblclick();e.stopPropagation(); break;
			case 37: _this.h(cell, false); break;
			case 38: _this.v(cell, false); break;
			case 9:  $(cell).focus(); break;
			case 39: _this.h(cell, true); break;
			case 40: _this.v(cell, true); break;
			case 32: $(cell).click(); break;
			default: //alert(e.keyCode);
		}
	};
	
	this.bind = function(unbind) {
		if (unbind == true) {
			_this.grid.getDataCells().unbind('keydown', this.keyPressed);
		} else {
			_this.grid.getDataCells().bind('keydown', this.keyPressed);
		}
	};
	this.detach = function() {
		this.bind(true);
		this.grid.pageChanged.remove(this.bind);
	};
	this.attach = function(grid) {
		this.grid = grid;
		this.grid.pageChanged.add(this.bind);
		this.bind();
	};
}; CellNavigateHandler.inherits(DatagridPlugin);